﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XGame.Tools;

namespace XGame.Class
{
    public class AniPic
    {
        public enum State
        {
            Stand, Run, Combat, Magic, Die
        }
        struct PicControler
        {
            public State State { get; set; }
            public int DirectionCount
            {
                get { return System.Enum.GetNames(typeof(Utility.Direction)).Length; }
            }
            public int PicCount
            {
                get { return 23; }
            }
            public int StateMin
            {
                get
                {
                    switch (State)
                    {
                        case AniPic.State.Stand:
                            return 0;
                        case AniPic.State.Run:
                            return 3;
                        case AniPic.State.Combat:
                            return 9;
                        case AniPic.State.Magic:
                            return 14;
                        case AniPic.State.Die:
                            return 20;
                        default:
                            return 0;
                    }
                }
            }
            public int StateMax
            {
                get
                {
                    switch (State)
                    {
                        case AniPic.State.Stand:
                            return 2;
                        case AniPic.State.Run:
                            return 8;
                        case AniPic.State.Combat:
                            return 13;
                        case AniPic.State.Magic:
                            return 19;
                        case AniPic.State.Die:
                            return 22;
                        default:
                            return 2;
                    }
                }
            }
            public PicControler(State state)
                : this()
            {
                this.State = state;
            }
        }

        public Utility.Direction Direction { get; set; }
        private PicControler picControler;
        private int runCurrent;
        Texture2D[,] texture;
        public AniPic(ContentManager content, string folderName)
        {
            this.picControler = new PicControler(State.Stand);
            this.runCurrent = this.picControler.StateMin;
            texture = new Texture2D[picControler.DirectionCount, picControler.PicCount];
            for (int i = 0; i < picControler.DirectionCount; i++)
            {
                for (int j = 0; j < picControler.PicCount; j++)
                {
                    texture[i, j] = content.Load<Texture2D>(string.Format(@"Image\Role\Body\{0}\{1}-{2}", folderName, i, j));
                }
            }
        }
        public void Animate()
        {
            if (++runCurrent == this.picControler.StateMax + 1)
                runCurrent = this.picControler.StateMin;
        }
        public Texture2D Show()
        {
            return texture[(int)this.Direction, runCurrent];
        }
        public State CurrentState
        {
            get { return this.picControler.State; }
            set 
            {
                if (this.picControler.State != value)
                {
                    this.picControler.State = value;
                    this.runCurrent = this.picControler.StateMin;
                }
            }
        }
    }
}
